package com.example.demo.controller;

import com.example.demo.Service.ICategoryService;
import com.example.demo.entity.Category;
import com.example.demo.entity.vo.CategoryListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@CrossOrigin(origins = "*")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/list")
    public Map<String, Object> list() {

        List<Category> categories = categoryService.list();

        List<CategoryListVo> categoryListVos = new ArrayList<>();

        for (Category category:categories) {
            CategoryListVo vo = new CategoryListVo();
            BeanUtils.copyProperties(category, vo);
//            vo.setCategoryId(category.getCategoryId());
//            vo.setCategoryName(category.getCategoryName());
            categoryListVos.add(vo);
        }

        Map<String, Object> map = new HashMap<>();

        map.put("data", categoryListVos);
        map.put("code", 200);
        map.put("msg", "查询成功");

        return map;

    }
}
